/*
 * This software is a confidential and proprietary information
 * of Kewill Trade & Logistics. You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with the company.
 */

/**
 * @author suryakand.shinde
 * @date 1st Feb 2008
 */

package com.search.dynamics.samples.spring.dao;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SDSessionFactory {
	static Logger log = Logger.getLogger(SessionFactory.class);
	private static org.hibernate.SessionFactory sessionFactory=null;
	
	public static SessionFactory getInstance() {
		if (sessionFactory == null )
			initSessionFactory();
		return sessionFactory;
	}

	public Session openSession() {
		return ((SDSessionFactory) sessionFactory).getCurrentSession();
	}

	public Session getCurrentSession() {
		return ((SDSessionFactory) sessionFactory).getCurrentSession();
	}

	private static synchronized void initSessionFactory() { 
		try{
			Configuration config = new Configuration();
			config.configure();
			sessionFactory = config.buildSessionFactory(); 
		}catch(HibernateException hexp){
			log.warn("Error while configuring hibernation session");
			log.error("Error ISF.001",hexp);
		}
	}

	public static void close() {
		if (sessionFactory != null)
			sessionFactory.close();
		sessionFactory = null;
	}
}